$(function () {
    // 需求一  获取并渲染文章类别信息
    // 封装成函数，后面的添加/修改/删除要复用！！
    initArtCateList()

    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates'
        }).then(({
            data: res
        }) => {
            if (res.status != 0) {
                return layui.layer.msg(res.message)
            }
            let arr = []
            res.data.forEach(ele => {
                if (ele.Id < 0) {
                    return
                }
                arr.push(
                    `   <tr>
                    <td>${ele.Id}</td>
                    <td>${ele.name}</td>
                    <td>${ele.alias}</td>
                    <td>
                        <button data-id='${ele.Id}' class="layui-btn btn-edit layui-btn-xs">修改</button>
                        <button  data-id='${ele.Id}' class=" btn-del layui-btn layui-btn-xs layui-btn-danger">删除</button>
                    </td>
                </tr>`
                )

            });
            $('tbody').empty().html(arr.join(''))
        })
    }

    // 需求二 添加分类功能
    let indexAdd = 0
    $('#addBtn').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ["500px", "260px"],
            content: `<form class="layui-form" id="addFrom">
            <!-- 分类的名字 -->
            <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required="" lay-verify="required|name" placeholder="请输入分类名称" autocomplete="off" class="layui-input">
            </div>
            </div>
            <!-- 分类的别名 -->
            <div class="layui-form-item">
            <label class="layui-form-label">分类别名</label>
            <div class="layui-input-block">
                <input type="text" name="alias" required="" lay-verify="required|alias" placeholder="请输入分类别名" autocomplete="off" class="layui-input">
        </div>
            </div>
            <!-- 按钮 -->
            <div class="layui-form-item">
        <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">确认添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
            </div>
        </form>`
        })
        // console.log(indexAdd);
    })
    // 需求三 添加文章分类，利用事件委托
    $('body').on("submit", '#addFrom', function (e) {
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(({
            data: res
        }) => {
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.msg('恭喜您，添加文章分类成功！')
            initArtCateList()
            // layui自带的关闭功能
            layer.close(indexAdd)

        })
    })
    // 需求四 修改功能,利用时间委托
    let indexEdit = 0
    $('tbody').on('click', '.btn-edit', function () {
        indexEdit = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ["500px", "260px"],
            content: `<form class="layui-form" id="editFrom" lay-filter="editForm">
            <input type="hidden" name="Id">
            <!-- 分类的名字 -->
            <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required="" lay-verify="required|name"
                autocomplete="off" class="layui-input">
            </div>
            </div>
            <!-- 分类的别名 -->
            <div class="layui-form-item">
            <label class="layui-form-label">分类别名</label>
            <div class="layui-input-block">
                <input type="text" name="alias" required="" lay-verify="required|alias" autocomplete="off" class="layui-input">
        </div>
            </div>
            <!-- 按钮 -->
            <div class="layui-form-item">
        <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">确认修改</button>
            </div>
            </div>
        </form>`
        })
        let id = $(this).attr('data-id')
        axios({
            method: 'GET',
            url: '/my/article/cates/' + id,
        }).then((
            res
        ) => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            // 成功后渲染
            layui.form.val('editForm', res.data.data)
        })
    })

    // 功能五 修改提交
    $('body').on("submit", '#editFrom', function (e) {
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(({
            data: res
        }) => {
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.msg('恭喜您，修改文章分类成功！')
            initArtCateList()
            // layui自带的关闭功能
            layer.close(indexEdit)
        })
    })

    // 功能六  删除功能
    $('tbody').on('click', '.btn-del', function () {
        let id = $(this).attr('data-id')
        layer.confirm('是否确认删除文章分类?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            axios({
                method: 'GET',
                url: '/my/article/deletecate/' + id,
            }).then((
                res
            ) => {
                layer.msg(res.data.message)
                initArtCateList()
            })
            // 关闭询问框
            layer.close(index);
        });

    })
})